*Use Kemahlioglu SESP 2015 replication dataset

*matching 2011 and 2015 districts
replace ilcekodu=. if code==9 & ilcekodu==0 & year==2015
replace ilcekodu=0 if code==9 & ilceadi=="EFELER"

replace ilcekodu=. if code==10 & ilcekodu==0 & year==2015
sort code year ilcekodu
replace akpshare=[akpshare+akpshare[_n-1]]/2 if code==10 & ilceadi=="ALTIEYLUL"
replace chpshare=[chpshare+chpshare[_n-1]]/2 if code==10 & ilceadi=="ALTIEYLUL"
replace mhpshare=[mhpshare+mhpshare[_n-1]]/2 if code==10 & ilceadi=="ALTIEYLUL"
replace hdpshare=[hdpshare+hdpshare[_n-1]]/2 if code==10 & ilceadi=="ALTIEYLUL"
replace votes=[votes+votes[_n-1]] if code==10 & ilceadi=="ALTIEYLUL"
replace rate=[rate+rate[_n-1]] if code==10 & ilceadi=="ALTIEYLUL"
replace okumayazmabilmeyen18=[okumayazmabilmeyen18+okumayazmabilmeyen18[_n-1]] if code==10 & ilceadi=="ALTIEYLUL"
replace toplam18=[toplam18+toplam18[_n-1]] if code==10 & ilceadi=="ALTIEYLUL"

replace ilcekodu=0 if code==10 & ilceadi=="ALTIEYLUL"


replace ilcekodu=. if code==20 & ilcekodu==0 & year==2015
sort code year ilcekodu
replace akpshare=[akpshare+akpshare[_n+1]]/2 if code==20 & ilceadi=="PAMUKKALE"
replace chpshare=[chpshare+chpshare[_n+1]]/2 if code==20 & ilceadi=="PAMUKKALE"
replace mhpshare=[mhpshare+mhpshare[_n+1]]/2 if code==20 & ilceadi=="PAMUKKALE"
replace hdpshare=[hdpshare+hdpshare[_n+1]]/2 if code==20 & ilceadi=="PAMUKKALE"
replace votes=[votes+votes[_n+1]] if code==20 & ilceadi=="PAMUKKALE"
replace rate=[rate+rate[_n+1]] if code==20 & ilceadi=="PAMUKKALE"
replace okumayazmabilmeyen18=[okumayazmabilmeyen18+okumayazmabilmeyen18[_n+1]] if code==20 & ilceadi=="PAMUKKALE"
replace toplam18=[toplam18+toplam18[_n+1]] if code==20 & ilceadi=="PAMUKKALE"

replace ilcekodu=0 if code==20 & ilceadi=="PAMUKKALE"

replace ilcekodu=. if code==31 & ilcekodu==0 & year==2015
replace ilcekodu=13 if code==31 & ilceadi=="ANTAKYA" 
replace ilcekodu=12 if code==31 & ilceadi=="ARSUZ" 
sort code year ilcekodu
replace akpshare=[akpshare+akpshare[_n+1]]/2 if code==31 & ilceadi=="ANTAKYA"
replace chpshare=[chpshare+chpshare[_n+1]]/2 if code==31 & ilceadi=="ANTAKYA"
replace mhpshare=[mhpshare+mhpshare[_n+1]]/2 if code==31 & ilceadi=="ANTAKYA"
replace hdpshare=[hdpshare+hdpshare[_n+1]]/2 if code==31 & ilceadi=="ANTAKYA"
replace votes=[votes+votes[_n+1]] if code==31 & ilceadi=="ANTAKYA"
replace rate=[rate+rate[_n+1]] if code==31 & ilceadi=="ANTAKYA"
replace okumayazmabilmeyen18=[okumayazmabilmeyen18+okumayazmabilmeyen18[_n+1]] if code==31 & ilceadi=="ANTAKYA"
replace toplam18=[toplam18+toplam18[_n+1]] if code==31 & ilceadi=="ANTAKYA"

replace ilcekodu=0 if code==31 & ilceadi=="ANTAKYA"
replace ilcekodu=13 if code==31 & ilceadi=="ARSUZ"

replace ilcekodu=. if code==45 & ilcekodu==0 & year==2015
sort code year ilcekodu
replace akpshare=[akpshare+akpshare[_n+1]]/2 if code==45 & ilceadi=="YUNUSEMRE"
replace chpshare=[chpshare+chpshare[_n+1]]/2 if code==45 & ilceadi=="YUNUSEMRE"
replace mhpshare=[mhpshare+mhpshare[_n+1]]/2 if code==45 & ilceadi=="YUNUSEMRE"
replace hdpshare=[hdpshare+hdpshare[_n+1]]/2 if code==45 & ilceadi=="YUNUSEMRE"
replace votes=[votes+votes[_n+1]] if code==45 & ilceadi=="YUNUSEMRE"
replace rate=[rate+rate[_n+1]] if code==45 & ilceadi=="YUNUSEMRE"
replace okumayazmabilmeyen18=[okumayazmabilmeyen18+okumayazmabilmeyen18[_n+1]] if code==45 & ilceadi=="YUNUSEMRE"
replace toplam18=[toplam18+toplam18[_n+1]] if code==45 & ilceadi=="YUNUSEMRE"

replace ilcekodu=0 if code==45 & ilceadi=="YUNUSEMRE"

replace ilcekodu=. if code==46 & ilcekodu==0 & year==2015
sort code year ilcekodu
replace akpshare=[akpshare+akpshare[_n+1]]/2 if code==46 & ilceadi=="ONIKISUBAT"
replace chpshare=[chpshare+chpshare[_n+1]]/2 if code==46 & ilceadi=="ONIKISUBAT"
replace mhpshare=[mhpshare+mhpshare[_n+1]]/2 if code==46 & ilceadi=="ONIKISUBAT"
replace hdpshare=0 if code==46 & ilceadi=="ONIKISUBAT"
replace hdpshare=[hdpshare+hdpshare[_n+1]]/2 if code==46 & ilceadi=="ONIKISUBAT"
replace votes=[votes+votes[_n+1]] if code==46 & ilceadi=="ONIKISUBAT"
replace rate=[rate+rate[_n+1]] if code==46 & ilceadi=="ONIKISUBAT"
replace okumayazmabilmeyen18=[okumayazmabilmeyen18+okumayazmabilmeyen18[_n+1]] if code==46 & ilceadi=="ONIKISUBAT"
replace toplam18=[toplam18+toplam18[_n+1]] if code==46 & ilceadi=="ONIKISUBAT"

replace ilcekodu=0 if code==46 & ilceadi=="ONIKISUBAT"

replace ilcekodu=14 if code==44 & ilcekodu==0 & year==2011
replace ilcekodu=15 if code==44 & ilcekodu==4 & year==2011
replace ilcekodu=16 if code==44 & ilcekodu==13 & year==2011
sort code year ilcekodu
replace akpshare=[akpshare+akpshare[_n+1]+akpshare[_n+2]]/3 if code==44 & ilcekodu==14 & year==2011
replace chpshare=[chpshare+chpshare[_n+1]+chpshare[_n+2]]/3 if code==44 & ilcekodu==14 & year==2011
replace mhpshare=[mhpshare+mhpshare[_n+1]+mhpshare[_n+2]]/3 if code==44 & ilcekodu==14 & year==2011
replace hdpshare=[hdpshare+hdpshare[_n+1]+hdpshare[_n+2]]/3 if code==44 & ilcekodu==14 & year==2011
replace votes=[votes+votes[_n+1]+votes[_n+2]] if code==44 & ilcekodu==14 & year==2011
replace rate=[rate+rate[_n+1]+rate[_n+2]] if code==44 & ilcekodu==14 & year==2011

replace ilcekodu=0 if code==44 & ilcekodu==14 & year==2011
replace ilcekodu=. if code==44 & ilcekodu==15 & year==2011
replace ilcekodu=. if code==44 & ilcekodu==16 & year==2011

replace ilcekodu=. if code==44 & ilcekodu==0 & year==2015
replace ilcekodu=15 if code==44 & ilcekodu==4 & year==2015
replace ilcekodu=16 if code==44 & ilcekodu==13 & year==2015
sort code year ilcekodu
replace akpshare=[akpshare+akpshare[_n+1]]/2 if code==44 & ilcekodu==15 & year==2015
replace chpshare=[chpshare+chpshare[_n+1]]/2 if code==44 & ilcekodu==15 & year==2015
replace mhpshare=[mhpshare+mhpshare[_n+1]]/2 if code==44 & ilcekodu==15 & year==2015
replace hdpshare=[hdpshare+hdpshare[_n+1]]/2 if code==44 & ilcekodu==15 & year==2015
replace votes=[votes+votes[_n+1]] if code==44 & ilcekodu==15 & year==2015
replace rate=[rate+rate[_n+1]] if code==44 & ilcekodu==15 & year==2015
replace okumayazmabilmeyen18=[okumayazmabilmeyen18+okumayazmabilmeyen18[_n+1]] if code==44 & ilcekodu==15 & year==2015
replace toplam18=[toplam18+toplam18[_n+1]] if code==44 & ilcekodu==15 & year==2015

replace ilcekodu=0 if code==44 & ilcekodu==15 & year==2015
replace ilcekodu=. if code==44 & ilcekodu==16 & year==2015

replace ilcekodu=. if code==47 & ilcekodu==0 & year==2015
replace ilcekodu=0 if code==47 & ilceadi=="ARTUKLU"

replace ilcekodu=. if code==48 & ilcekodu==0 & year==2015
replace ilcekodu=0 if code==48 & ilceadi=="MENTESE"

replace ilcekodu=. if code==52 & ilcekodu==0 & year==2015
replace ilcekodu=0 if code==52 & ilceadi=="ALTINORDU"

replace ilcekodu=. if code==59 & ilcekodu==0 & year==2015
replace ilcekodu=0 if code==59 & ilceadi=="SULEYMANPASA"

replace ilcekodu=. if code==61 & ilcekodu==0 & year==2015
replace ilcekodu=0 if code==61 & ilceadi=="ORTAHISAR"

replace ilcekodu=. if code==63 & ilcekodu==0 & year==2015
sort code year ilcekodu
replace akpshare=[akpshare+akpshare[_n+1]+akpshare[_n+2]]/3 if code==63 & ilceadi=="EYYUBIYE"
replace chpshare=[chpshare+chpshare[_n+1]+chpshare[_n+2]]/3 if code==63 & ilceadi=="EYYUBIYE"
replace mhpshare=[mhpshare+mhpshare[_n+1]+mhpshare[_n+2]]/3 if code==63 & ilceadi=="EYYUBIYE"
replace hdpshare=[hdpshare+hdpshare[_n+1]+hdpshare[_n+2]]/3 if code==63 & ilceadi=="EYYUBIYE"
replace votes=[votes+votes[_n+1]+votes[_n+2]] if code==63 & ilceadi=="EYYUBIYE"
replace rate=[rate+rate[_n+1]+rate[_n+2]] if code==63 & ilceadi=="EYYUBIYE"
replace okumayazmabilmeyen18=[okumayazmabilmeyen18+okumayazmabilmeyen18[_n+1]+okumayazmabilmeyen18[_n+2]] if code==63 & ilceadi=="EYYUBIYE"
replace toplam18=[toplam18+toplam18[_n+1]+toplam18[_n+2]] if code==63 & ilceadi=="EYYUBIYE"

replace ilcekodu=0 if code==63 & ilceadi=="EYYUBIYE"
replace ilcekodu=. if code==63 & ilcekodu==12 & year==2015
replace ilcekodu=. if code==63 & ilcekodu==13 & year==2015

replace ilcekodu=. if code==65 & ilcekodu==0 & year==2015
sort code year ilcekodu
replace akpshare=[akpshare+akpshare[_n+1]]/2 if code==65 & ilceadi=="IPEKYOLU"
replace chpshare=[chpshare+chpshare[_n+1]]/2 if code==65 & ilceadi=="IPEKYOLU"
replace mhpshare=[mhpshare+mhpshare[_n+1]]/2 if code==65 & ilceadi=="IPEKYOLU"
replace hdpshare=0 if code==65 & ilceadi=="IPEKYOLU"
replace hdpshare=[hdpshare+hdpshare[_n+1]]/2 if code==65 & ilceadi=="IPEKYOLU"
replace votes=[votes+votes[_n+1]] if code==65 & ilceadi=="IPEKYOLU"
replace rate=[rate+rate[_n+1]] if code==65 & ilceadi=="IPEKYOLU"
replace okumayazmabilmeyen18=[okumayazmabilmeyen18+okumayazmabilmeyen18[_n+1]] if code==65 & ilceadi=="IPEKYOLU"
replace toplam18=[toplam18+toplam18[_n+1]] if code==65 & ilceadi=="IPEKYOLU"

replace ilcekodu=0 if code==65 & ilceadi=="IPEKYOLU"


*Lagged vote shares
sort code ilcekodu year
generate akplag=akpshare[_n-1] if ilcekodu[_n]==ilcekodu[_n-1] & code[_n]==code[_n-1] & ilcekodu~=.
generate chplag=chpshare[_n-1] if ilcekodu[_n]==ilcekodu[_n-1] & code[_n]==code[_n-1] & ilcekodu~=.
generate mhplag=mhpshare[_n-1] if ilcekodu[_n]==ilcekodu[_n-1] & code[_n]==code[_n-1] & ilcekodu~=.
generate bgmzlag=bgmzshare[_n-1] if ilcekodu[_n]==ilcekodu[_n-1] & code[_n]==code[_n-1] & ilcekodu~=.

*Illiteracy rate in 2013
generate illiteracy2013=okumayazmabilmeyen18/toplam18

*Logged population 
generate lnpop=ln(toplam18)

*Difference in unemployment rate
gen unempdif=unemp-unemp11

*New businesses
gen toplamacilan=firmnew15+coopnew15+indnew15
gen toplamacilan14=firmnew14+coopnew14+indnew14
gen netacilan=toplamacilan-indnew15-firmliq15-coopliq15-firmclos15-coopclos15-indclos15
gen netacilan14=toplamacilan14-indnew14-firmliq14-coopliq14-firmclos14-coopclos14-indclos14
gen acilandif=toplamacilan-toplamacilan14
gen netacilandif=netacilan-netacilan14
generate netacilanth=netacilan/1000

*Local government control
egen mayorvote=rowmax( mhdapar- mbmz)
generate akpcontrol15=1 if makpart==mayorvote & mayorvote~=.
replace akpcontrol15=0 if akpcontrol15~=1 & year==2015 & ilcekodu~=. & makpart~=.
generate chpcontrol15=1 if mchp==mayorvote & mayorvote~=.
replace chpcontrol15=0 if chpcontrol15~=1 & year==2015 & ilcekodu~=. & mchp~=.
generate mhpcontrol15=1 if mmhp==mayorvote & mayorvote~=.
replace mhpcontrol15=0 if mhpcontrol15~=1 & year==2015 & ilcekodu~=. & mmhp~=.
generate hdpcontrol15=1 if mhdp==mayorvote & mayorvote~=.
replace hdpcontrol15=1 if mbdp==mayorvote & mayorvote~=.
replace hdpcontrol15=0 if hdpcontrol15~=1 & year==2015 & ilcekodu~=. & makpart~=.


*analysis (Table Columns 1-3 and 5)
cd C:\data
reg akpshare akplag illiteracy2013 lnpop unemp unempdif netacilanth akpcontrol15 akpprovcompet, cluster(code)
outreg2 using sepstable, bdec(2) excel replace 
reg chpshare chplag illiteracy2013 lnpop unemp unempdif netacilanth chpcontrol15 chpprovcompet onsecim, cluster(code)
outreg2 using sepstable, bdec(2) excel append
reg mhpshare mhplag illiteracy2013 lnpop unemp unempdif netacilanth mhpcontrol15 mhpprovcompet, cluster(code)
outreg2 using sepstable, bdec(2) excel append
reg hdpshare bgmzlag illiteracy2013 lnpop unemp unempdif netacilanth hdpcontrol15 bgmzprovcompet, cluster(code)
outreg2 using sepstable, bdec(2) excel append

